package com.blue.controller.ai;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blue.common.constant.CommonConstant;
import com.blue.common.entity.LoginUserInfo;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ThreadLocalUtils;
import com.blue.dto.ai.ChatHistoryDTO;
import com.blue.po.ai.ChatHistoryPO;
import com.blue.service.ai.IChatHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * AI聊天历史记录表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2024-08-23
 */
@RestController
@RequestMapping("ai/chatHistory")
@Tag(name = "AI聊天历史记录表")
public class ChatHistoryController {
    @Autowired
    private IChatHistoryService chatHistoryService;

    @GetMapping("/list")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list() {
        LoginUserInfo loginUserInfo = ThreadLocalUtils.getLoginUserInfo();
        String userId = loginUserInfo.getUserId();
        List<ChatHistoryPO> chatHistories = chatHistoryService.list(new LambdaQueryWrapper<ChatHistoryPO>()
                .eq(ChatHistoryPO::getCreatorId, userId)
                .eq(ChatHistoryPO::getIsFlag, CommonConstant.YES)
                .orderByDesc(ChatHistoryPO::getCreateTime));
        return ResponseResult.okResult(chatHistories);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody ChatHistoryDTO requestDTO) {
        chatHistoryService.add(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody ChatHistoryDTO requestDTO) {
        chatHistoryService.update(new LambdaUpdateWrapper<ChatHistoryPO>()
                .set(ChatHistoryPO::getIsFlag, CommonConstant.NO)
                .eq(ChatHistoryPO::getId, requestDTO.getId()));
        return ResponseResult.okResult();
    }
}
